package com.example.enjoyarkuix;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.enjoyarkuix.stagefragment.NativeFragment;
import com.example.enjoyarkuix.stagefragment.OneFragment;
import com.google.android.material.tabs.TabLayout;

public class FragmentManagerActivity extends AppCompatActivity {
    OneFragment oneFragment;
    NativeFragment nativeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_manager);
        Log.i("HiHelloWorld", "FragmentManagerActivity");

        ImageView ivTopBarBack = (ImageView) findViewById(R.id.ivTopBarBack);
        ivTopBarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView title = (TextView) findViewById(R.id.tvTopBarTitle);
        title.setText("原生Activity");

        oneFragment = new OneFragment();
        Bundle args = new Bundle();
        args.putString("params", "{\"params\":[{\"key\":\"content\",\"type\":10,\"value\":\"hello,stageFragment\"}，{\"key\":\"from\",\"type\":10,\"value\":\"消息来自activity\"}]}");
        oneFragment.setArguments(args);

        nativeFragment = new NativeFragment();

        TabLayout tabLayout = findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab().setText("StageFragment"));
        tabLayout.addTab(tabLayout.newTab().setText("原生Fragment"));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                int position = tab.getPosition();
                Log.i("HiHelloWorld", "FragmentManagerActivity onTabSelected:" + position);
                if (position == 0) {
                    if (oneFragment.isAdded()) {
                        transaction.hide(nativeFragment);
                        transaction.show(oneFragment).commitNowAllowingStateLoss();
                    } else {
                        transaction.add(R.id.app_fragment_container, oneFragment);
                    }
                } else {
                    transaction.hide(oneFragment);
                    if (nativeFragment.isAdded()) {
                        transaction.show(nativeFragment).commitNowAllowingStateLoss();
                    } else {
                        transaction.add(R.id.app_fragment_container, nativeFragment);
                    }
                }
                transaction.commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 当标签从选中变为未选中时调用
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 当标签已经被选中，再次被点击时调用
            }
        });

        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().add(R.id.app_fragment_container, oneFragment).commit();
    }
}